import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;

public class PlayerManager implements Serializable {

	private Vector<Player> playerList = null;
	private int tableNumber;
	private int count = 0;
	private int coordinateCount = 0;
	private static Map<Integer, PlayerPosition> cordinates = new HashMap<Integer, PlayerPosition>();
	private Map<Integer, PlayerPosition> bufferedList = new HashMap<Integer, PlayerPosition>();
	
	/*
	 * Loads the coordinates at start-up.
	 */
	static {
		cordinates.put(1, new PlayerPosition(370, 120));
		cordinates.put(2, new PlayerPosition(210, 280));
		cordinates.put(3, new PlayerPosition(295, 410));
		cordinates.put(4, new PlayerPosition(500, 485));
		cordinates.put(5, new PlayerPosition(780, 470));
		cordinates.put(6, new PlayerPosition(900, 400));
		cordinates.put(7, new PlayerPosition(1000, 300));
		cordinates.put(8, new PlayerPosition(900, 120));
	}

	public PlayerManager(int initialTableNumber, int initialCount) {
		tableNumber = initialTableNumber;
		count = initialCount;
		if (playerList == null)
			playerList = new Vector<Player>();
	}

	public synchronized void addPlayer(String name, Card[] Cards) {
		boolean isDealer = false;
		boolean isCurrPlayer = false;

		count++;
		coordinateCount++;
		if (count == 1)
			isDealer = true;
		else
			isDealer = false;

		if (count == 2)
			isCurrPlayer = true;
		else
			isCurrPlayer = false;

		getPlayerCoordinate();
		playerList.addElement(new Player(name, Cards, tableNumber, isDealer,
				isCurrPlayer, bufferedList.get(coordinateCount)));

	}

	/**
	 * This method manipulates on which chair the new player will sit. It goes
	 * in anti-clockwise direction (based on the x and y pixels in {cordinates}
	 * Map) and fills up the left seats in anti-clockwise fashion if players in
	 * between go out of game.
	 */
	private void getPlayerCoordinate() {
		if (coordinateCount < 8) {
			for (Iterator<Integer> playerPosIter = cordinates.keySet()
					.iterator(); playerPosIter.hasNext();) {
				Integer key = playerPosIter.next();
				if (cordinates.get(key) == null) {
					coordinateCount = key;
				}
			}
		}
		bufferedList.put(coordinateCount, cordinates.remove(coordinateCount));//remove when people go.
	}

	public void removePlayer(Player player) {
		playerList.removeElement(player);
		cordinates.put(coordinateCount, bufferedList.get(coordinateCount));
		count--;
	}

	public void foldPlayer(Player player) {
		player.setState("folded");
	}

	public Player getPlayer(String name) {
		Player playerObject = null;
		Player result = null;
		String playerName = null;
		Iterator<Player> itr = playerList.iterator();
		// Find the player with this name
		while (itr.hasNext()) {
			playerObject = itr.next();
			playerName = playerObject.getName();
			if (playerName.equals(name))
				result = playerObject;
		}
		return (result);
	}

	public Player getDealer() {
		Player playerObject = null;
		Player result = null;
		Iterator<Player> itr = playerList.iterator();
		// Find the player with this name
		while (itr.hasNext()) {
			playerObject = itr.next();
			if (playerObject.getIsDealer())
				result = playerObject;
		}
		return (result);

	}

	public Player getCurrentPlayer() {
		Player playerObject = null;
		Player result = null;
		Iterator<Player> itr = playerList.iterator();
		// Find the current player
		while (itr.hasNext()) {
			playerObject = itr.next();
			if (playerObject.getIsCurrPlayer())
				result = playerObject;
		}
		return (result);
	}

	public synchronized void updateCurrentPlayer(Player player) {
		// Get the next player and set it as the current player
		Player newCurrPlayer = null;
		if ((playerList.lastElement()).equals(player)) {
			Iterator<Player> iter = playerList.iterator();
			while (iter.hasNext()) {
				newCurrPlayer = iter.next();
				if (!newCurrPlayer.getState().equals("Folded")
						&& !newCurrPlayer.getState().equals("Queued"))
					break;
			}
			// newCurrPlayer = playerList.firstElement();
			newCurrPlayer.setIsCurrPlayer(true);
			player.setIsCurrPlayer(false);
		} else {
			int currPlayerIndex = playerList.indexOf(player);
			int index = currPlayerIndex + 1;
			while (index != currPlayerIndex) {
				newCurrPlayer = playerList.get(index);
				if (!newCurrPlayer.getState().equals("Folded")
						&& !newCurrPlayer.getState().equals("Queued")) {
					newCurrPlayer.setIsCurrPlayer(true);
					player.setIsCurrPlayer(false);
					break;
				}
				index = (index + 1) % playerList.size();
			}
		}
	}

	public int getTableNumber() {
		return tableNumber;
	}

	public synchronized int getCountofPlayers() {
		return count;
	}

	public Vector<Player> getListOfPlayers() {
		return playerList;
	}

}
